package com.yonyou.cms.epc.controller.graphic;
import com.yonyou.cms.epc.client.dto.graphic.GraphicModelDTO;
import com.yonyou.cms.epc.client.entity.CarModelPO;
import com.yonyou.cms.epc.client.entity.graphic.GraphicModelPO;
import com.yonyou.cms.epc.client.feign.BaseDataClient;
import com.yonyou.cms.epc.service.graphic.GraphicModelService;
import com.yonyou.cyx.framework.bean.dto.framework.RestResultResponse;
import com.yonyou.cyx.framework.util.bean.ApplicationContextHelper;
import com.yonyou.cyxdms.framework.bean.dto.user_info.LoginInfoDto;
import org.springframework.web.bind.annotation.*;
import com.yonyou.cyx.framework.controller.base.BaseController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;

/**
 *
 * @author yzy
 * @since 2022-06-08
 */
@RestController
@RequestMapping("/graphic/graphicModel")
public class GraphicModelController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(GraphicModelController.class);

    @Autowired
    public GraphicModelService graphicModelService;
    @Autowired
    public BaseDataClient baseDataClient;

    /**
     * 分页查询数据
     *
     * @param queryCondition 查询条件
     * @return com.baomidou.mybatisplus.core.metadata.IPage 分页对象
     * @author yzy
     * @since 2022-06-08
     */
    @ResponseBody
    @GetMapping("query")
    @SuppressWarnings("unchecked")
    public IPage<Map<String,Object>> query (@RequestParam Map<String,Object> queryCondition, @RequestParam
        ("currentPage") int currentPage, @RequestParam("pageSize") int pageSize) {
        logger.debug("执行了分页查询车型系统",queryCondition,currentPage,pageSize);
        Page<Map<String,Object>> page = new Page(currentPage, pageSize);
        return graphicModelService.selectPageBysql(page, queryCondition);
    }

    /**
     * 获取车型列表
     *
     * @param
     * @return com.baomidou.mybatisplus.core.metadata.IPage 分页对象
     * @author yzy
     * @since 2022-06-08
     */
    @ResponseBody
    @GetMapping("getList")
    @SuppressWarnings("unchecked")
    public RestResultResponse<List<CarModelPO>> getList (@RequestParam Map<String, Object> queryCondition) {
        logger.debug("执行了获取车型系统列表:"+queryCondition.get("modelCode"));
        LoginInfoDto loginInfo = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        queryCondition.put("appId",loginInfo.getAppId());
        RestResultResponse<List<CarModelPO>> listRestResultResponse = baseDataClient.queryModelBySql(queryCondition);
        return listRestResultResponse;
    }

    /**
     * 根据id查询
     *
     * @param modelId 数据主键ID
     * @return  Map<String,Object> 数据对应的map 结构
     * @author yzy
     * @since 2022-06-08
     */
    @ResponseBody
    @GetMapping("selectById")
    @SuppressWarnings("unchecked")
    public Map<String,Object> getById(@RequestParam Integer modelId) {
        logger.debug("执行了根据Id查询modelId:{}",modelId);
        return graphicModelService.getById(modelId);
    }


    /**
     * 进行数据新增
     *
     * @param graphicModelDtoList 需要保存的车型集合
     * @return Map<String,Object> 返回的数据map
     * @author yzy
     * @since 2022-06-08
     */
    @ResponseBody
    @PostMapping("insert")
    @SuppressWarnings("unchecked")
    public boolean insert(@RequestBody List<GraphicModelPO> graphicModelDtoList) {
        logger.debug("执行了批量新增车型graphicModelDtoList:{}",graphicModelDtoList);
        return graphicModelService.insertBatch( graphicModelDtoList);
    }

    /**
     * 进行数据修改
     *
     * @param id 需要修改数据的ID
     * @param graphicModelDTO 需要保存的DTO
     * @return Map<String,Object> 返回的数据map
     * @author yzy
     * @since 2022-06-08
     */
    @ResponseBody
    @PostMapping(value="/updateById/{id}")
    @ResponseStatus(HttpStatus.CREATED)
    @SuppressWarnings("unchecked")
    public Map<String,Object> updateByid(@PathVariable("id") Integer id, @RequestBody GraphicModelDTO graphicModelDTO) {
        logger.debug("执行了修改graphicModelDTO:{}",graphicModelDTO);
        return graphicModelService.update(id,graphicModelDTO);
    }

    /**
     * 根据IDs批量删除对象
     *
     * @param ids 需要修改数据的ID集合
     * @author yzy
     * @since 2022-06-08
     */
    @PostMapping(value="/batch/{ids}")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public boolean deleteByIds(@PathVariable("ids") String ids){
        logger.debug("执行了批量删除,删除ID:{}",ids);
        graphicModelService.deleteBatchIds(ids);
        return true;
    }

    /**
     * 根据id删除对象
     *
     * @param id 需要修改数据的ID
     * @author yzy
     * @since 2022-06-08
     */
    @DeleteMapping(value="/{id}")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public boolean deleteById(@PathVariable("id") Integer id){
        logger.debug("删除ID:{}",id);
        graphicModelService.deleteById(id);
        return true;
    }

    /**
     * 批量导入
     *
     * @author xh
     * @since 2022-7-7
     */
    @ResponseBody
    @PostMapping("imports")
    @ResponseStatus(HttpStatus.CREATED)
    public Map<String, Object> imports(@RequestBody List<Map<String, Object>> datas) {
        logger.info("批量导入图解车型:{}", datas);
        return graphicModelService.imports(datas);
    }




}